var util = require('../../utils/util.js');
var refresh = false;
Page({
  data: {
    version: 'V1.7.0',
    dfh: 0,
    yfh: 0,
    ysh: 0
  },
  onLoad: function (options) {
    refresh = options.refresh || false;
    var that = this;
    wx.showLoading({
      title: '数据请求中',
    });
    if (refresh) {
      that.searchOrders();
    } else {
      if (util.get('dfh', 'outTime') == 'outTime') {
        that.searchOrders();
      } else {
        // console.log('【我的】页面数据调用缓存。');
        wx.hideLoading();
        that.setData({
          dfh: wx.getStorageSync('dfh'),
          yfh: wx.getStorageSync('yfh'),
          ysh: wx.getStorageSync('ysh')
        })
      }
    }
  },
  onShow(){
    var that = this;
    that.setData({
      identity_id: wx.getStorageSync('identity_id')||'1'
    })
  },
  // 查询订单数据
  searchOrders() {
    var that = this;
    let times = 120;
    wx.cloud.callFunction({
      name: "OrderFunctions",
      data: {
        type: 'myOrder',
        mold: 'my'
      },
      success: res => {
        wx.hideLoading();
        // console.log(res);
        that.setData({
          dfh: res.result[0].total,
          yfh: res.result[1].total,
          ysh: res.result[2].total,
        });
        // 缓存times秒
        util.put('dfh', res.result[0].total, times);
        util.put('yfh', res.result[1].total, times);
        util.put('ysh', res.result[2].total, times);
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    this.onLoad(refresh);
    wx.stopPullDownRefresh();
  },
   // 赞赏支持
   showQrcode() {
    wx.previewImage({
      urls: ['cloud://ops-6gsfwaqa93cc5203.6f70-ops-6gsfwaqa93cc5203-1252250366/system_image/zanshang.jpg'],
      current: 'cloud://ops-6gsfwaqa93cc5203.6f70-ops-6gsfwaqa93cc5203-1252250366/system_image/zanshang.jpg' // 当前显示图片的http链接      
    })
  },
  // 清空缓存
  clearStorage() {
    wx.showModal({
      title: '清空缓存',
      content: '确定清空小程序缓存吗？',
      success: res => {
        if (res.confirm) {
          wx.clearStorage({
            success: (res) => {
              wx.showToast({
                title: '已清空缓存',
                icon: 'success'
              }, 2000);
              // 延时跳转
              setTimeout(function () {
                wx.switchTab({
                  url: '../index/index',
                })
              }, 2000)
            },
          })
        }
      }
    })
  },
  // 隐藏模态框
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 选择微信头像
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
  },
  // 登录小程序 
  getUserProfile(e) {
    var that = this;
    that.setData({
      modalName: 'infoModal'
    })
  },
  // 用户注册信息上传
  userRegister(e) {
    if (e.detail.value.nick == '' || this.data.avatarUrl == null) {
      wx.showToast({
        icon: 'error',
        title: '头像或昵称未设置',
      })
    } else {
      this.setData({
        avatarUrl: this.data.avatarUrl,
        nick: e.detail.value.nick,
        modalName: null
      });
    }
  },
})